package gofish.json;

/**
 * JsonObjectElement(s) are ultimately used as leaf nodes in JsonObject objects.
 * @author Matt Lauber
 *
 */
public class JsonObjectElement extends AbstractJson {

	/**
	 * Since this is represents a JSON object, it needs both a key and a value.
	 * The key instance variable obviously holds the key for this key-value pair.
	 * Key will be stored pre-formatted, as "\"key\":"  
	 */
	private String key;
	/**
	 * value is of the generic type Object so it can hold Boolean, String,
	 * Integer, Double, Character, an AbsractCompositeJson, or a String
	 * representation of null.
	 */
	private Object value;
	
	/**
	 * There is one constructor for each of the classes mentioned above.
	 * Each one sets up value so that, when toString() is called, value
	 * will be displayed in appropriate JSON format. 
	 * @param v : will be assigned to the value instance variable.
	 */
	public JsonObjectElement(String k, String v) {
		setKey(k);
		this.value = "\"" + v + "\"";
	}
	
	public JsonObjectElement(String k, char v) {
		setKey(k);
		this.value = "\"" + String.valueOf(v) + "\"";
	}
	
	public JsonObjectElement(String k, int v) {
		setKey(k);
		this.value = new Integer(v);
	}
	
	public JsonObjectElement(String k, double v) {
		setKey(k);
		this.value = new Double(v);
	}
	
	public JsonObjectElement(String k, boolean v) {
		setKey(k);
		this.value = new Boolean(v);
	}
	
	public JsonObjectElement(String k, AbstractCompositeJson v) {
		setKey(k);
		this.value = v;
	}
	
	public JsonObjectElement(String k) {
		setKey(k);
		//this.value = null;
		this.value = new String("null"); // so that when this element is printed, it prints 'null' rather
		                                 //   throw an Exception for trying to print a null object.
	}
	
	/**
	 * toString() is overridden so that it just returns the
	 * String representation of key and value concatenated together.
	 */
	public String toString() {
		return key + value.toString();
	}
	
	/**
	 * setKey() is a private setter for the key attribute.
	 * @param key
	 */
	private void setKey(String key) {
		this.key = "\"" + key + "\":";
	}
}
